import { reqUserAddress, reqOrderInfo } from "@/api";
import { ORDER_INFO, USER_ADDRESS } from '@/store/mutation-types'

export default {
  namespaced: true,
  state: {
    address: [],
    orderInfo: {}
  },
  actions: {
    async getUserAddress({ commit }) {
      try {
        const res = await reqUserAddress()
        if (res.code == 200) {
          commit(USER_ADDRESS, res.data)
          return Promise.resolve('成功')
        }
        throw res.message
      } catch (error) {
        return Promise.reject(error)
      }

    },
    async getOrderInfo({ commit }) {
      try {
        const res = await reqOrderInfo()
        if (res.code == 200) {
          commit(ORDER_INFO, res.data)
          return Promise.resolve('成功')
        }
        throw res.message
      } catch (error) {
        return Promise.reject(error)
      }
    }
  },
  mutations: {
    [USER_ADDRESS]: (context, data) => context.address = data,
    [ORDER_INFO]: (context, data) => context.orderInfo = data
  },
  getters: {
    getDetailList(state) {
      return state.orderInfo.detailArrayList
    }
  }
}